package com.talkyun.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseConverter {

    public Date toDate(Object obj) {
        if (obj instanceof Date) {
            return (Date) obj;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(obj.toString());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public Integer toInteger(Object obj) {
        Class<?> clazz = obj.getClass();
        if (clazz == Integer.class || clazz == int.class) {
            return (Integer) obj;
        }
        return Integer.parseInt(obj.toString());
    }

    public Boolean toBoolean(Object obj) {
        Class<?> clazz = obj.getClass();
        if (clazz == Boolean.class || clazz == boolean.class) {
            return (Boolean) obj;
        }
        return Boolean.parseBoolean(obj.toString());
    }

    public long toLong(Object obj) {
        Class<?> clazz = obj.getClass();
        if (clazz == Long.class || clazz == long.class) {
            return (Long) obj;
        }
        return Long.parseLong(obj.toString());
    }

    public String toString(Object obj) {
        Class<?> clazz = obj.getClass();
        if (clazz == String.class) {
            return (String) obj;
        }
        return obj.toString();
    }
}